#!/usr/bin/env python
# -*- coding: utf-8 -*-

import matplotlib.lines as lines
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import rcParams

rcParams['font.family'] = 'sans-serif'
rcParams['font.sans-serif'] = ['HarmonyOS Sans SC', 'Microsoft YaHei']
rcParams['font.size'] = 24

data = (
    {
        'xlab': '记录下限阈值 R',
        'x': range(1, 8),
        'hr': (0.818699, 0.848574, 0.844567, 0.831292, 0.824858, 0.819203, 0.809401),
        'pr': (0.454761, 0.459151, 0.534830, 0.614485, 0.625154, 0.618991, 0.660483),
        'hrlim': (65, 90),
        'prlim': (0, 100),
        'output': 'figures/FigA_min_support.png'
    },
    {
        'xlab': '记录上限阈值 S',
        'x': (4, 6, 8, 10, 12, 14, 16),
        'hr': (0.847998, 0.848796, 0.848574, 0.848277, 0.848892, 0.849478, 0.848917),
        'pr': (0.447273, 0.448960, 0.449151, 0.446562, 0.446304, 0.447260, 0.447454),
        'hrlim': (81, 86),
        # 'prlim': (41.5, 45.5),
        'prlim': (38, 48),
        'output': 'figures/FigB_max_support.png'
    },
    {
        'xlab': r'前瞻距离 $\Delta$',
        'x': (2, 5, 8, 10, 15, 20, 50, 100),
        'hr': (0.882597, 0.870286, 0.858296, 0.848574, 0.835405, 0.831675, 0.814373, 0.802935),
        'pr': (0.579591, 0.491844, 0.467725, 0.459151, 0.447527, 0.462751, 0.498071, 0.533229),
        'hrlim': (65, 90),
        'prlim': (0, 100),
        'output': 'figures/FigC_delta.png'
    },
    {
        'xlab': r'预取大小 P',
        'x': (0, 1, 2, 3, 5, 8, 10, 20, 50),
        'hr': (0.689621, 0.787836, 0.806842, 0.819168, 0.833160, 0.848574, 0.857139, 0.861904, 0.864418),
        'pr': (None, 0.815566, 0.727786, 0.658931, 0.549804, 0.459151, 0.399086, 0.259146, 0.123402),
        'hrlim': (50, 100),
        'prlim': (0, 100),
        'output': 'figures/FigD_prefetch_size.png'
    },
    {
        'xlab': '挖掘列表长度 N',
        'x': (10, 30, 50, 75, 100, 150, 200, 300),
        'hr': (0.854001, 0.853239, 0.848574, 0.837378, 0.821608, 0.812130, 0.822522, 0.814605),
        'pr': (0.517180, 0.481226, 0.449151, 0.404841, 0.373133, 0.371628, 0.378531, 0.375722),
        'hrlim': (65, 90),
        'prlim': (0, 100),
        'output': 'figures/FigE_mining_size.png'
    },
    {
        'xlab': '记录列表长度 RSize',
        'x': (100, 200, 400, 600, 800, "1k", "5k", "10k", "20k"),
        'hr': (0.846538, 0.848239, 0.848220, 0.848498, 0.848355, 0.848574, 0.848574, 0.848574, 0.848574),
        'pr': (0.428094, 0.445671, 0.447913, 0.446576, 0.447330, 0.449151, 0.449151, 0.449151, 0.449151),
        'hrlim': (81, 86),
        'prlim': (38, 48),
        'output': 'figures/FigF_record_size.png'
    },
    {
        'xlab': r'记录前瞻长度 $\Delta_R$',
        'x': (0, 1, 2, 3, 5, 10, 15, 20, 25),
        'hr': (0.756622, 0.788765, 0.879181, 0.882688, 0.874252, 0.848574, 0.846253, 0.844974, 0.844340),
        'pr': (0.301926, 0.245222, 0.270161, 0.373405, 0.409185, 0.449151, 0.458442, 0.458226, 0.460665),
        'hrlim': (65, 90),
        'prlim': (10, 60),
        'output': 'figures/FigG_emith_delta.png'
    }
)


def main_hr_pr(
    hr=(), hrlim=None, hrcol='0.15',
    pr=(), prlim=None, prcol='0',
    xlab='', x=None, output=None
):
    fig = plt.figure(figsize=(9, 6))  # create figure
    ax = plt.axes(position=[0.125, 0.15, 0.75, 0.75])  # create axes
    ax2 = ax.twinx()  # create yyaxis

    l = max(len(hr), len(pr))
    style_hr = {'color': '#00000010', 'edgecolor': hrcol, 'linewidth': 2.5}
    style_pr = {'linewidth': 3, 'marker': 'o', 'markersize': 15, 'color': prcol}

    nhr = np.array(hr, dtype=float).transpose() * 100  # to 100%
    ax.bar(range(l), nhr, label='缓存命中率', ** style_hr)
    ax.plot([], [], label='预取准确率', **style_pr)  # make an agent
    if hrlim:
        ax.set_ylim(hrlim)
    ax.set_ylabel('缓存命中率 (%)')

    npr = np.array(pr, dtype=float).transpose() * 100  # to 100%
    ax2.plot(range(l), npr, label='预取准确率', **style_pr)
    if prlim:
        ax2.set_ylim(prlim)
    ax2.set_ylabel('预取准确率 (%)')

    ax.set_xticks(range(l))
    if x:
        ax.set_xticklabels(x)

    ax.set_xlabel(xlab)
    ax.legend(bbox_to_anchor=(0.5, 1.16), loc=9, ncol=2, edgecolor='1')

    if type(output) == str:
        if False:
            fig.add_artist(lines.Line2D([0, 1], [0, 1]))
            fig.add_artist(lines.Line2D([0, 1], [1, 0]))
        plt.savefig(output)
    pass


# main_hr_pr(**data[0])
# main_hr_pr(**data[1])
# main_hr_pr(**data[2])
# main_hr_pr(**data[3])
# main_hr_pr(**data[4])
# main_hr_pr(**data[5])
main_hr_pr(**data[6])
